package com.example.springboottest.example.atomic;

import com.example.springboottest.util.Print;
import com.example.springboottest.util.ThreadUtil;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;

public class AtomicIntegerArrayTest {
    static final int THREAD_COUNT = 5;

    public static void main(String[] args) {
        int tempvalue = 0;
        //原始的数
        int[] array = {1, 2, 3, 4, 5, 6};

        //包装为原子数组
        AtomicIntegerArray i = new AtomicIntegerArray(array);
        //获取第0个元素，然后设置为2
        tempvalue = i.getAndSet(0, 2);
        //输出tempvalue:1; i:[2, 2, 3, 4, 5, 6]
        Print.fo("tempvalue:" + tempvalue + "; i:" + i);
        //获取第0个元素，然后自增
        tempvalue = i.getAndIncrement(0);
        //输出tempvalue:2; i:[3, 2, 3, 4, 5, 6]
        Print.fo("tempvalue:" + tempvalue + "; i:" + i);
        //获取第0个元素，然后增加一个delta 5
        tempvalue = i.getAndAdd(0, 5);
        //输出tempvalue:3; i:[8, 2, 3, 4, 5, 6] 
        Print.fo("tempvalue:" + tempvalue + "; i:" + i);
    }
}
